package cn.gailvlun.gll.presentation.xroom

import android.content.Context
import android.content.Intent
import android.graphics.drawable.Drawable
import android.os.Bundle
import android.support.design.widget.BottomSheetBehavior
import android.support.design.widget.BottomSheetDialog
import android.support.design.widget.CoordinatorLayout
import android.view.LayoutInflater
import android.view.View
import android.view.WindowManager
import android.webkit.WebView
import android.webkit.WebViewClient
import cn.gailvlun.gll.R
import cn.gailvlun.gll.presentation.base.BaseActivity
import cn.gailvlun.gll.util.ProfileUtil
import cn.gailvlun.gll.util.SPUtil
import cn.gailvlun.gll.util.ToastUtil
import com.blankj.utilcode.util.ConvertUtils
import com.blankj.utilcode.util.ScreenUtils
import com.bumptech.glide.Glide
import com.bumptech.glide.request.target.SimpleTarget
import com.bumptech.glide.request.transition.Transition
import kotlinx.android.synthetic.main.activity_cross_room_introduce.*

class CrossRoomIntroduceActivity : BaseActivity() {
    companion object {
        fun openActivity(context: Context) {
            val intent = Intent(context, CrossRoomIntroduceActivity::class.java)
            context.startActivity(intent)
        }
    }

    override fun initComponent() {
        setContentView(R.layout.activity_cross_room_introduce)

        Glide.with(mContext)
                .load(R.drawable.img_cross_room_introduce)
                .into(object: SimpleTarget<Drawable>() {
                    override fun onResourceReady(resource: Drawable, transition: Transition<in Drawable>?) {
                        ll_bg.setBackgroundDrawable(resource)
                    }
                })
    }

    override fun createHandler() {
        nbv.setOnBackListener {
            onBackPressed()
        }

        btn_participate.setOnClickListener {
            
            val profile = ProfileUtil.getProfile()
            if (profile.isGender != null) {
                
                CrossRoomGameInfoActivity.openActivity(mContext)
            } else {
                

            }
        }

        tv_rule.setOnClickListener {
            
            openRuleIntroduceDialog()
        }
    }

    override fun loadData(savedInstanceState: Bundle?) {
        val hasAttendCrossRoom = SPUtil.getBoolean(SPUtil.USER, "hasAttendCrossRoom")

        if (!hasAttendCrossRoom) {
            
            openRuleIntroduceDialog()
        }
    }

    private fun openRuleIntroduceDialog() {
        SPUtil.put(SPUtil.USER, "hasAttendCrossRoom", true)

        BottomSheetDialog(mContext, R.style.CustomBottomSheetDialogTheme)
                .apply {
                    LayoutInflater.from(mContext).inflate(R.layout.dialog_cross_room_rule_introduce, null).apply {
                        setContentView(this)

                        findViewById<CoordinatorLayout>(R.id.cl).apply {
                            layoutParams.height = ScreenUtils.getScreenHeight() - ConvertUtils.dp2px(128f)
                        }

                        BottomSheetBehavior.from(this.parent as View).apply {
                            peekHeight = ScreenUtils.getScreenHeight() - ConvertUtils.dp2px(138f)
                        }
                    }
                }
                .show()
    }

}